package com.wsoft.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wsoft.service.IMemberCacheService;
import com.wsoft.service.IMemberCommonService;
import com.wsoft.system.entity.SysAccountEntity;
import com.wsoft.system.entity.SysMemberEntity;
import com.wsoft.system.entity.SysRoleEntity;
import com.wsoft.system.service.ISysAccountService;
import com.wsoft.system.service.ISysMemberOrgRoleRelationService;
import com.wsoft.system.service.ISysMemberService;
import com.wsoft.vo.LoginUserDetail;
import com.wsoft.vo.LoginUserVO;
import com.wsoft.vo.RoleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述
 *
 * @author: juiet
 * @date: 2024年09月03日 10:37
 */
@Service
public class MemberCommonServiceImpl implements IMemberCommonService {
    @Resource
    private IMemberCacheService memberCacheService;
    @Resource
    private ISysMemberService sysMemberService;
    @Resource
    private ISysAccountService sysAccountService;
    @Resource
    private ISysMemberOrgRoleRelationService sysMemberOrgRoleRelationService;

    @Override
    public UserDetails loadUserByUsername(String username) {
        //1、优先取缓存数据
        LoginUserDetail loginUserDetail = memberCacheService.getCacheUser(username);
        if (loginUserDetail instanceof LoginUserDetail) {
            return loginUserDetail;
        }
        //2、缓存没有就重新数据库查出来
        LambdaQueryWrapper<SysAccountEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysAccountEntity::getLoginName, username);
        SysAccountEntity sysAccountEntity = sysAccountService.getOne(queryWrapper);
        if (sysAccountEntity == null) {
            return null;
        }
        SysMemberEntity sysMemberEntity = sysMemberService.getById(sysAccountEntity.getMemberId());
        LoginUserVO loginUserVO = new LoginUserVO();
        BeanUtils.copyProperties(sysAccountEntity, loginUserVO, "id");
        BeanUtils.copyProperties(sysMemberEntity, loginUserVO);
        loginUserVO.setAccountId(sysAccountEntity.getId());

        // 查询角色信息
        List<SysRoleEntity> roleList = sysMemberOrgRoleRelationService.getRoleListByMember(sysAccountEntity.getMemberId());
        if (CollUtil.isNotEmpty(roleList)) {
            List<RoleVO> roleVOList = roleList.stream()
                    .map(e -> Convert.convert(RoleVO.class, e))
                    .collect(Collectors.toList());
            loginUserVO.setRoles(roleVOList);
        }

        // todo 资源等下
        loginUserDetail = new LoginUserDetail(loginUserVO, new ArrayList<>());

        //3、重新查出来的重新放入缓存
        memberCacheService.cacheMember(loginUserDetail);
        return loginUserDetail;
    }
}
